package com.sisgh.jefdv1.cmd.command.has.multi.impl;

import com.sisgh.jefdv1.cmd.command.has.arg.Arguments;
import com.sisgh.jefdv1.cmd.command.has.arg.argument.impl.OptionMarkerArgument;
import com.sisgh.jefdv1.cmd.command.has.arg.argument.properties.ArgumentProperties;
import com.sisgh.jefdv1.cmd.command.has.multi.MultiArgsCommand;
import com.sisgh.jefdv1.core.exec.Executor;
import com.sisgh.jefdv1.core.exec.impl.MySQLExecutor;
import com.sisgh.jefdv1.core.jdbc.connection.ConnectionFactoryBuilder;
import com.sisgh.jefdv1.core.jdbc.connection.factory.ConnectionFactory;
import com.sisgh.jefdv1.core.jdbc.connection.properties.ConnectionProperties;
import com.sisgh.jefdv1.core.jdbc.db.Database;
import com.sisgh.jefdv1.exception.cmd.exec.ExecuteCommandException;

import java.sql.SQLException;

public class ConnectionCommand extends MultiArgsCommand {

    private static final ArgumentProperties[] APS = {
            new ArgumentProperties("type", "t", true),
            new ArgumentProperties("host", "h", true),
            new ArgumentProperties("port", "p", true),
            new ArgumentProperties("user", "u", true),
            new ArgumentProperties("password", "p", true),
            new ArgumentProperties("database", "db", true),
    };

    private static final String CMD = "connection";

    public ConnectionCommand() {
        super(Arguments.options(APS));
    }

    @Override
    protected String getCmd() {
        return CMD;
    }

    private Executor buildExecutor(Database db, ConnectionProperties cp) throws ExecuteCommandException {
        try {
            ConnectionFactory factory = ConnectionFactoryBuilder.getConnectionFactory(db);
            return switch (db) {
                case MYSQL -> MySQLExecutor.newInstance(factory, cp);
                default -> throw new ExecuteCommandException("不支持的数据库");
            };
        } catch (SQLException e) {
            throw new ExecuteCommandException(db.getName() + "数据库连接异常", e);
        }
    }

    @Override
    public Executor execute(Executor executor) throws ExecuteCommandException {
        // 通过 type 判断是哪个类型的 数据库
        String type = args.get("type").values().get(0);
        Database database = Database.valueOf(type);

        // 创建 url
        String host = args.get("host").values().get(0);
        String port = args.get("port").values().get(0);
        String db = args.get("database").values().get(0);

        String url;
        try {
            url = database.buildUrl(host, port, db);
        } catch (SQLException e) {
            throw new ExecuteCommandException(e.getMessage(), e);
        }

        // 创建 cp
        String user = args.get("user").values().get(0);
        String password = args.get("password").values().get(0);

        ConnectionProperties cp = new ConnectionProperties(url, user, password);

        // 创建 Executor
        return buildExecutor(database, cp);
    }

    @Override
    protected boolean isArgName(String arg) {
        return OptionMarkerArgument.checker().check(arg);
    }
}
